package leoner.multithread4;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MockMultiThreadServiceImpl implements MockMultiThreadService {
	private static ExecutorService threadExcutor = Executors.newFixedThreadPool(10);
//	private   ExecutorService threadExcutor;
	@Override
	public String m1(Integer id, String name) {
		System.out.println("m1 invoke  "+"id:"+id+";name:"+name);
		MockGetUrlBean bean = new MockGetUrlBean();
		Callable<String> callble = new GetUrlCallable<String>(String.class,"fetchUrl",bean,"http://www.google.com");
		String result = "empty";
		try {
			 result = threadExcutor.submit(callble).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("m1:"+id+ "  done==>"+result);
		return "";
	}

	@Override
	public Integer m2(Integer id) {
		System.out.println("m2 invoke  "+"id:"+id);
		MockGetUrlBean bean = new MockGetUrlBean();
		Callable<String> callble = new GetUrlCallable<String>(String.class,"fetchUrl",bean,"http://www.baidu.com");
		String result = "empty";
		try {
			 result = threadExcutor.submit(callble).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("m2:"+id+ "  done==>"+result);

		return 0;
	}
	
//	public void setThreadExcutor(ExecutorService threadExcutor) {
//		this.threadExcutor = threadExcutor;
//	}


}
